// Copyright 2013 The Go Circuit Project
// Use of this source code is governed by the license for
// The Go Circuit Project, found in the LICENSE file.
//
// Authors:
//   2013 Petar Maymounkov <p@gocircuit.org>

package ns

import (
	"circuit/kit/rh"
	"fmt"
	"unsafe"
)

// Server
type Server struct {
	slash rh.FID
}

func New(slash Dir) *Server {
	return &Server{slash: NewFID("/", slash)}
}

func (srv *Server) SignIn(user, _ string) (rh.Session, error) {
	return &Session{
		srv:   srv,
		user:  user,
		slash: srv.slash,
	}, nil
}

func (srv *Server) String() string {
	return fmt.Sprintf("ns_%02x", uint64(uintptr(unsafe.Pointer(srv))))
}

// Session
type Session struct {
	srv   *Server
	user  string
	slash rh.FID
}

func (ssn *Session) String() string {
	return fmt.Sprintf("%s_%02x", ssn.srv.String(), uint64(uintptr(unsafe.Pointer(ssn))))
}

func (ssn *Session) Walk(wname []string) (rh.FID, error) {
	return ssn.slash.Walk(wname)
}

func (ssn *Session) SignOut() {}
